#!/bin/bash
source ~/.bashrc

# LINE=`ps -ef | grep task.sh | wc -l`
# echo "task start "$LINE
# if [ $LINE -le 4 ]; then
cd /home/renkeju/cuda/neural-style-pt
PIDFILE=./task.pid
SERVER=43.228.182.78
if [ ! -f $PIDFILE ];then
  echo "check last task"
  trap "rm -f -- '$PIDFILE'" EXIT
  echo $$ > "$PIDFILE"

  FILE=`curl http://$SERVER:3000/task/json`
  echo $FILE | jq '.data'
  
  ID=`echo $FILE|jq -r '.data.id'`
  INPUT=`echo $FILE|jq -r '.data.input'`
  STYLE=`echo $FILE|jq -r '.data.style'`
  USER=`echo $FILE|jq -r '.data.user'`
  FILENAME=`echo ${INPUT##*/}`
  if [ $ID != 'null' ]; then
    echo "./main.sh" $ID $STYLE $FILENAME
    source main.sh $ID $STYLE $FILENAME
  fi
else
  date
  echo `cat $PIDFILE`" runing" 
fi